package com.jeff.snake.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class TileView extends View {

	protected static final int X_TILE_COUNT = 24;
	protected static final int Y_TILE_COUNT = 40;
	private static final int WIDTH = 20;
	private static final int HEIGHT = 20;

	private Bitmap[] mColorArray;
	private int[][] mGridTile = new int[X_TILE_COUNT][Y_TILE_COUNT];

	public TileView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	protected void setColorTiles(int count) {
		mColorArray = new Bitmap[count];
	}

	protected void loadTile(int id, Drawable drawable) {
		Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, WIDTH, HEIGHT);
		drawable.draw(canvas);
		mColorArray[id] = bitmap;
	}

	protected void setGridTile(int color, int x, int y) {
		mGridTile[x][y] = color;
	}

	protected void clearGridTile() {
		for (int i = 0; i < X_TILE_COUNT; i++) {
			for (int j = 0; j < Y_TILE_COUNT; j++) {
				mGridTile[i][j] = 0;
			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		for (int i = 0; i < mGridTile.length; i++) {
			for (int j = 0; j < mGridTile[i].length; j++) {
				if (mGridTile[i][j] != 0) {
					canvas.drawBitmap(mColorArray[mGridTile[i][j]], i * WIDTH,
							j * HEIGHT, null);
				}
			}
		}
	}

}
